package com.bike.util.qrcode;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import javax.imageio.ImageIO;

import org.apache.commons.lang3.StringUtils;

import com.bike.util.Constant;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.topit.show.aliapi.AlyOssFileUpLoadUtils;

import net.coobird.thumbnailator.Thumbnails;

/**
 * 画制定logo和制定描述的二维码
 * 
 * @author songyz
 *
 */
public class ZXingCode {
    private static final int QRCOLOR = 0xFF000000; // 默认是黑色
    private static final int BGWHITE = 0xFFFFFFFF; // 背景颜色

    private static final int WIDTH = 600; // 二维码宽
    private static final int HEIGHT = 600; // 二维码高

    // 用于设置QR二维码参数
    private static Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>() {
        private static final long serialVersionUID = 1L;
        {
            put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);// 设置QR二维码的纠错级别（H为最高级别）具体级别信息
            put(EncodeHintType.CHARACTER_SET, "utf-8");// 设置编码方式
            put(EncodeHintType.MARGIN, 0);
        }
    };

    public static void main(String[] args) throws Exception {
//        File logoFile = new File("D://dizhu//huhulogo.png");
//        File QrCodeFile = new File("D://dizhu/2013-01.png");
//        String url = "https://www.baidu.com/";
//        drawLogoQRCode(logoFile, QrCodeFile, url);
        //createDining();
    	
    	String logoFilePath="D://dizhu//huhulogo.png";
		String filePath="D://dizhu/2013-01.jpg";
		String bgFilePath="D://dizhu/bg.jpg";
		String ggFilePath="D://dizhu/bg2.jpg";
		
		try {
//			AlyOssFileUpLoadUtils alyOssFileUpLoadUtils = new AlyOssFileUpLoadUtils();
//			alyOssFileUpLoadUtils.fileUpload(alyFileuppath , new File(Constant.filePath+alyFileuppath));//---暂时没有删除本地图片
			ZXingCode.createDining(logoFilePath,  "https://www.baidu.com/", bgFilePath, new File(filePath),new File(ggFilePath),"柚子时光酒店","305");
		} catch (IOException e) {
			e.printStackTrace();
		}
    	
    }

    // 生成带logo的二维码图片
    public static BufferedImage drawLogoQRCode(File logoFile, String qrUrl) {
        try {
            MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
            // 参数顺序分别为：编码内容，编码类型，生成图片宽度，生成图片高度，设置参数
            BitMatrix bm = multiFormatWriter.encode(qrUrl, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hints);
            BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);

            // 开始利用二维码数据创建Bitmap图片，分别设为黑（0xFFFFFFFF）白（0xFF000000）两色
            for (int x = 0; x < WIDTH; x++) {
                for (int y = 0; y < HEIGHT; y++) {
                    image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
                }
            }

            int width = image.getWidth();
            int height = image.getHeight();
            if (Objects.nonNull(logoFile) && logoFile.exists()) {
                // 构建绘图对象
                Graphics2D g = image.createGraphics();
                // 读取Logo图片
                BufferedImage logo = ImageIO.read(logoFile);
                // 开始绘制logo图片
                g.drawImage(logo, width * 2 / 5, height * 2 / 5, width * 2 / 10, height * 2 / 10, null);
                g.dispose();
                logo.flush();
            }
            return  image;
            // 自定义文本描述
        
//            ImageIO.write(image, "png", codeFile); // TODO
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  null;
    }
    
    public static  void createDining(String logoFilePath,String url ,String bgFilePath ,File diningFile,File sourceDiningFile,String businessName,String diningName) throws IOException{
    	 File logoFile = new File(logoFilePath);
    	BufferedImage image= drawLogoQRCode(logoFile,  url);//二维码
    	BufferedImage bg= ImageIO.read(new File(bgFilePath));//获取背景图片
    	Graphics2D g=bg.createGraphics();
    	int width=600;
    	int height=600;
    	
    	g.drawImage(image,(bg.getWidth()- width)/2,1100,width,height,null);
    	//画 酒店名称  桌牌名称
    	String text=businessName.concat(" ").concat(diningName);
    	
    	g.setColor(Color.WHITE);
		g.setFont(new Font("微软雅黑", Font.PLAIN, 60)); // 字体、字型、字号
		int textWidth=g.getFontMetrics().stringWidth(text);
		
    	g.drawString(text,(bg.getWidth()- textWidth)/2, 1780);
    	
    	g.dispose();
    	bg.flush();
    	image.flush();
    	ImageIO.write(bg,"png", sourceDiningFile);
    	
    	
    	
    	//压缩
    	Thumbnails.of(sourceDiningFile)
	     .scale(1f)
	     .outputQuality(0.2d)
	     .toFile(diningFile);
    }
}